import { createRouter, createWebHistory } from "vue-router";

/**
 * 重写路由的push方法
*/
const routerPush = createRouter.prototype.push
createRouter.prototype.push = function push(location) {
    if(typeof(location) == 'string'){
        let Separator = '&';
        if(location.indexOf('?') == -1) {
            Separator == '?';
        }
        location = location + Separator + 'random' + Math.random();
    }
    return routerPush.call(this, location).catch(error=> error)
}

const routes = [
    //登录
    {
        path: '/',
        name: '登录',
        component: () => import('@/views/Login/Login.vue'),
    },

    //首页
    {
        path: '/',
        component: () => import('@/components/navMain/navMain.vue'),
        meta:{
            requireAuth: true
        },
        children: [
            {
                path: '/Index',
                name: '首页',
                component: () => import('@/views/Index/Index.vue'),
                meta:{
                    requireAuth: true
                }
            },
        ]
    },

    //团队管理
    {
        path: '/',
        component: () => import('@/components/navMain/navMain.vue'),
        meta:{
            requireAuth: true
        },
        children: [
            {
                path: '/TeamManage',
                name: '团队管理',
                component: () => import('@/views/TeamManage/TeamManage.vue'),
                meta:{
                    requireAuth: true
                }
            },
            {
                path: '/TeamUser',
                name: '团队用户',
                component: () => import('@/views/TeamManage/TeamUser.vue'),
                meta:{
                    requireAuth: true
                }
            },
        ]
    },

    //系统管理
    {
        path: '/',
        component: () => import('@/components/navMain/navMain.vue'),
        meta:{
            requireAuth: true
        },
        children: [
            {
                path: '/MenuManage',
                name: '菜单管理',
                component: () => import('@/views/SystemManage/MenuManage.vue'),
                meta:{
                    requireAuth: true
                }
            },
            {
                path: '/DataManage',
                name: '数据字典',
                component: () => import('@/views/SystemManage/DataManage.vue'),
                meta:{
                    requireAuth: true
                }
            },
        ]
    },

    //404
    {
        path: '/404',
        name: '404',
        component: () => import('@/views/SinglePage/404.vue'),
    },
];

//创建路由
const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes,
});

// 全局守卫
router.beforeEach((to, from, next) => {
    let token = localStorage.getItem("token");
    //判断访问登录页存在token时,跳转/Index
    if(to.path == '/' && token){
        next({
            path: '/Index'
        });
    }

    //判断路由是否需要权限
    if(to.matched.some(record => record.meta.requireAuth)){
        //判断token是否存在
        if(!token){
            next({
                path: '/',
                query: { redirect: to.fullPath }
            })
        }
    }

    //404跳转
    if(to.matched.length === 0){
        next({path: '/404'})
    }else{
        next();
    }
});

export default router;